package cs2340.spring13.team3.wheresmystuff.model;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;

public class Tag extends ServerObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@JsonProperty("title")
	private String title;

	protected static String BASE_URL = "/tags/";

	public Tag() {
		this.setTitle("");
	}

	public Tag(String title) throws ClientProtocolException, IOException {
		om = new ObjectMapper();
		if (title.indexOf('{') != -1) {
			// json passed in, not a title
			Tag t = om.readValue(title, Tag.class);
			this.setTitle(t.getTitle());
		} else {
			this.setTitle(title);
			try {
				this.create(om.writeValueAsString(this));
			} catch (SyncException se) {
				se.printStackTrace();
			}
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getId() {
		return this.id;
	}

	public JsonNode toJson() {
		return om.valueToTree(this);
	}

	@Override
	public String getBaseURL() {
		return BASE_URL;
	}

	public boolean equals(Tag t) {
		return (title == t.getTitle());

	}
}
